#-*- coding: UTF-8 -*-
import re
import os
import devU
import json
import tm

#将多条数据，以list取出
def readsql(str):
	ss =str.split("),(")
	ss[0] = ss[0].strip("(")
	ss[-1] = ss[-1].strip(")")
	return ss

#解析s_workitem，用于匹配工作项id
def getSWorkitem(list0):
	newlist = list()
	for ilist in list0:
		ilist1= ilist.replace(",NULL,", ",'NULL',")
		ilistinfo = ilist1.split(",'")
		idict = {}
		idict["itemid"] = ilistinfo[0].strip("'")
		idict["itemname"] = ilistinfo[1].strip("'")
		idict["seqno"] = ilistinfo[2].strip("'")
		idict["artf"] = ilistinfo[3].strip("'")
		idict["developers"] = ilistinfo[4].strip("'")
		idict["owner"] = ilistinfo[5].split("'")[0]
		newlist.append(idict)
	return newlist

#解析s_standard_list
def getSStandardlist(list0):
	newlist = list()
	for ilist in list0:
		ilistinfo = ilist.split(",'")
		idict = {}
		idict["guid"] = ilistinfo[0].split(",")[0]
		idict["guidworkitem"] = ilistinfo[0].split(",")[1]
		idict["programname"] = ilistinfo[1].strip("'")
		idict["patchtype"] = ilistinfo[2].strip("'")
		idict["deploywhere"] = ilistinfo[3].strip("'")
		idict["fullpath"] = ilistinfo[4].strip("'")
		idict["partofproject"] = ilistinfo[5].strip("'")
		idict["committype"] = ilistinfo[6].strip("'")
		newlist.append(idict)
	return newlist

#标准清单all ，list0 s_standard_list，list1 s_workitem
def paSStandardlist(list0,list1):
	newlist = list()
	for ilist in list0:
		patchtypes = ilist["patchtype"].split(",")
		#针对入库错误数据，跳过
		if(len(patchtypes)>1):
			if ilist["deploywhere"].find(ilist["patchtype"])>=0:
				continue
		for patchtype in patchtypes:
			textstr = ilist["deploywhere"].replace('\\','')
			text = json.loads(textstr)
			#针对入库错误数据，跳过
			if patchtype in text:
				pass
			else:
				continue
			deploywheres = text[patchtype].split(",")
			for	deploywhere in deploywheres:
				idict = {}
				idict["workitemname"] = devU.gets(list1,"itemid",ilist["guidworkitem"],"itemname")
				idict["workitemid"] = ilist["guidworkitem"]
				idict["programname"] = ilist["programname"]
				idict["patchtype"] = patchtype
				idict["deploywhere"] = deploywhere
				if patchtype == "ecd":
					idict["fullpath"] = "/"+devU.ecdPath(ilist["fullpath"],ilist["partofproject"])
				else:
					idict["fullpath"] = "/"+devU.cutW(ilist["fullpath"],ilist["partofproject"])
				idict["partofproject"] = ilist["partofproject"]
				idict["committype"] = ilist["committype"]
				idict["owner"] = devU.gets(list1,"itemid",ilist["guidworkitem"],"owner")
				idict["bb1"] = "all"
				idict["bb2"] = "all"
				newlist.append(idict)
	return newlist

#标准清单按工作项 list0 s_standard_list，list1 s_workitem，workid 工作项id
def pbSStandardlist(list0,list1,workid):
	newlist = list()
	for ilist in list0:
		
		if ilist["guidworkitem"]!=workid:
			continue
		patchtypes = ilist["patchtype"].split(",")
		#针对入库错误数据，跳过
		if(len(patchtypes)>1):
			if ilist["deploywhere"].find(ilist["patchtype"])>=0:
				continue
		for patchtype in patchtypes:
			textstr = ilist["deploywhere"].replace('\\','')
			text = json.loads(textstr)
			#针对入库错误数据，跳过
			if patchtype in text:
				pass
			else:
				continue
			deploywheres = text[patchtype].split(",")
			for	deploywhere in deploywheres:
				idict = {}
				idict["workitemname"] = devU.gets(list1,"itemid",ilist["guidworkitem"],"itemname")
				idict["workitemid"] = ilist["guidworkitem"]
				idict["programname"] = ilist["programname"]
				idict["patchtype"] = patchtype
				idict["deploywhere"] = deploywhere
				if patchtype == "ecd":
					idict["fullpath"] = "/"+devU.ecdPath(ilist["fullpath"],ilist["partofproject"])
				else:
					idict["fullpath"] = "/"+devU.cutW(ilist["fullpath"],ilist["partofproject"])
				idict["partofproject"] = ilist["partofproject"]
				idict["committype"] = ilist["committype"]
				idict["owner"] = devU.gets(list1,"itemid",ilist["guidworkitem"],"owner")
				idict["bb1"] = "all"
				idict["bb2"] = "all"
				newlist.append(idict)
	return newlist
#针对多条
def pcSStandardlist(list0,workids):
	newlist = list()
	for ilist in list0:
		if ilist["workitemid"] in workids:
			newlist.append(ilist)		
	return newlist
#获取标准清单中的工作项信息，用于生成文件名，list0 s_standard_list，list1 s_workitem，arrsvn 账户dict
def getworkinfo(list0,list1,arrsvn):
	arr = {}
	for ilist in list0:
		workid = ilist["guidworkitem"]
		if workid in arr:
			pass
		else:
			workname = devU.gets(list1,"itemid",workid,"itemname")
			workowner = arrsvn[devU.gets(list1,"itemid",workid,"owner")]
			arr[workid] = formatfilename(workname+"-"+workowner+"-"+workid)
	return 	arr
	
def getworkinfofs(list0,list1,arrsvn):
	arr = {}
	for ilist in list0:
		workid = ilist["guidworkitem"]
		if workid in arr:
			pass
		else:
			workname = devU.gets(list1,"itemid",workid,"itemname")
			workowner = arrsvn[devU.gets(list1,"itemid",workid,"owner")]
			arr[workid] = formatfilename(workid+"-"+workname+"-"+workowner)
	return 	arr

#获取投放申请清单中的投放申请信息，用于生成文件名，list0 SDeliverylist，arrsvn 账户dict
def getdelinfo(list0,arrsvn):
	arr = {}
	for ilist in list0:
		applyid = ilist["guiddelivery"]
		if applyid in arr:
			pass
		else:
			applyname = ilist["applyalias"]
			applyowner = arrsvn[ilist["owner"]]
			arr[applyid] = formatfilename(applyname+"-"+applyowner+"-"+applyid)
	return 	arr
	
def getdelinfofs(list0,arrsvn):
	arr = {}
	for ilist in list0:
		applyid = ilist["guiddelivery"]
		if applyid in arr:
			pass
		else:
			applyname = ilist["applyalias"]
			applyowner = arrsvn[ilist["owner"]]
			arr[applyid] = formatfilename(applyid+"-"+applyowner+"-"+applyname)
	return 	arr

#解析s_delivery，用于匹配申请id
def getSDelivery(list0):
	newlist = list()
	for ilist in list0:
		ilist1= ilist.replace("'", "")
		ilistinfo = ilist1.split(",")
		ilistinfo1 = ilist.split("'")
		idict = {}
		idict["applyid"] = ilistinfo[0]
		idict["applyalias"] = ilistinfo1[1]

		dtimes = re.findall(r"(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})",ilist1)
		idict["applytime"] = tm.ftime8(dtimes[0])[0:10]
		idict["deliverytime"] = tm.ftime8(dtimes[1])[0:10]
		rec=re.compile(r'^(.*?)\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}(.*?)\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}(.*)')
		match=rec.match(ilist1)
		if match:
			idict["workitemid"] = match.group(1).split(",")[-5]
			idict["profilesid"] = match.group(1).split(",")[-4]
			idict["deliverytype"] = match.group(1).split(",")[-3]
			idict["proposer"] = match.group(1).split(",")[-2]
			
			idict["packtime"] = match.group(2).split(",")[-2]
			idict["deliveryresult"] = match.group(3)[1]
		newlist.append(idict)
	return newlist

#解析s_delivery_list
def getSDeliverylist(list0):
	newlist = list()
	for ilist in list0:
		ilistinfo = ilist.split(",'")
		idict = {}
		idict["guid"] = ilistinfo[0].split(",")[0]
		idict["guiddelivery"] = ilistinfo[0].split(",")[1]
		idict["programname"] = ilistinfo[1].strip("'")
		idict["patchtype"] = ilistinfo[2].strip("'")
		idict["deploywhere"] = ilistinfo[3].strip("'")
		idict["fullpath"] = ilistinfo[4].strip("'")
		idict["partofproject"] = ilistinfo[5].strip("'")
		idict["committype"] = ilistinfo[6].strip("'")
		idict["fromtype"] = ilistinfo[7].strip("'")
		newlist.append(idict)
	return newlist

#全量导出SDeliverylist ，list0 s_delivery_list ，list1 s_delivery，list2 s_workitem
def paSDeliverylist(list0,list1,list2):
	newlist = list()
	for ilist in list0:
		deliveryresult = devU.gets(list1,"applyid",ilist["guiddelivery"],"deliveryresult")
		if deliveryresult!="D":
			continue
		patchtypes = ilist["patchtype"].split(",")
		#针对入库错误数据，跳过
		if(len(patchtypes)>1):
			if ilist["deploywhere"].find(ilist["patchtype"])>=0:
				continue
		for patchtype in patchtypes:
			textstr = ilist["deploywhere"].replace('\\','')
			text = json.loads(textstr)
			#针对入库错误数据，跳过
			if patchtype in text:
				pass
			else:
				continue
			deploywheres = text[patchtype].split(",")
			for	deploywhere in deploywheres:
				idict = {}
				idict["guiddelivery"] = ilist["guiddelivery"]
				idict["workitemid"] = devU.gets(list1,"applyid",ilist["guiddelivery"],"workitemid")
				idict["applyalias"] = devU.gets(list1,"applyid",ilist["guiddelivery"],"applyalias")
				idict["profilesid"] = devU.gets(list1,"applyid",ilist["guiddelivery"],"profilesid")
				idict["packtime"] = devU.gets(list1,"applyid",ilist["guiddelivery"],"packtime")
				idict["deliverytime"] = devU.gets(list1,"applyid",ilist["guiddelivery"],"deliverytime")
				idict["deliveryresult"] = devU.gets(list1,"applyid",ilist["guiddelivery"],"deliveryresult")
				idict["workitemname"] = devU.gets(list2,"itemid",idict["workitemid"],"itemname")
				idict["programname"] = ilist["programname"]
				idict["patchtype"] = patchtype
				idict["deploywhere"] = deploywhere
				if patchtype == "ecd":
					idict["fullpath"] = "/"+devU.ecdPath(ilist["fullpath"],ilist["partofproject"])
				else:
					idict["fullpath"] = "/"+devU.cutW(ilist["fullpath"],ilist["partofproject"])
				idict["partofproject"] = ilist["partofproject"]
				idict["committype"] = ilist["committype"]
				idict["owner"] = devU.gets(list2,"itemid",idict["workitemid"],"owner")
				idict["bb1"] = "all"
				idict["bb2"] = "all"
				newlist.append(idict)
	return newlist
	


#按申请导出SDeliverylist list0 SDeliverylist ,applyque 条件
def pbSDeliverylist(list0,applyque):
	newlist = list()
	for ilist in list0:
		if "appid" in applyque:
			if ilist["guiddelivery"]!=applyque["appid"]:
				continue
		if "profilesid" in applyque:
			if ilist["profilesid"]!=applyque["profilesid"]:
				continue
		if "packtime" in applyque:
			if ilist["packtime"]!=applyque["packtime"]:
				continue
		if "deliverytime" in applyque:
			if ilist["deliverytime"]!=applyque["deliverytime"]:
				continue
		
		newlist.append(ilist)		
	return newlist
	
#按申请导出SDeliverylist list0 SDeliverylist ,applyid 条件
def pcSDeliverylist(list0,applyid):
	newlist = list()
	for ilist in list0:
		if ilist["guiddelivery"]==applyid:
			newlist.append(ilist)		
	return newlist
	
#按申请导出SDeliverylist list0 SDeliverylist ,applyids 条件
def pcSDeliverylist(list0,applyids):
	newlist = list()
	for ilist in list0:
		if ilist["guiddelivery"] in applyids:
			newlist.append(ilist)		
	return newlist

def getquekey(list0,kw):
	newlist = list()
	for ilist in list0:
		if kw in ilist:
			if ilist[kw] in newlist:
				pass
			else:
				newlist.append(ilist[kw])
	return newlist
				
		
#解析s_svn_account，ID-姓名，list0 s_svn_account
def svndict(list0):
	arr = {}
	for ilist in list0:
		ilist1= ilist.replace("'", "")
		ilistinfo = ilist1.split(",")
		id = ilistinfo[1]
		name = ilistinfo[2]
		if id in arr:
			pass
		else:
			arr[id] = name
	return 	arr
	
#去除文件名中的特殊字符
def formatfilename(str):
	return str.replace('"','').replace('\\','').replace('/','').replace(':','').replace('*','').replace('?','').replace('<','').replace('>','').replace('|','')

#环境id-代码	
def profilesdict(list0):
	arr = {}
	for ilist in list0:
		ilist1= ilist.replace("'", "")
		ilistinfo = ilist1.split(",")
		id = ilistinfo[0]
		profilescode = ilistinfo[1]
		if id in arr:
			pass
		else:
			arr[id] = profilescode
	return 	arr
	
